package com.micro.train.controller;

import com.micro.train.service.impl.TrainSeatServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 * 库存管理控制器
 * 提供手动触发库存管理功能的接口
 */
@RestController
@RequestMapping("/api/inventory")
public class InventoryController {

    @Autowired
    private TrainSeatServiceImpl trainSeatService;

    /**
     * 手动初始化指定列车的库存窗口
     */
    @PostMapping("/init/{trainId}")
    public ResponseEntity<Map<String, Object>> initializeInventory(@PathVariable Long trainId) {
        try {
            LocalDate today = LocalDate.now();
            trainSeatService.initializeInventoryWindow(trainId, today);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "库存初始化成功");
            response.put("trainId", trainId);
            response.put("baseDate", today.toString());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "库存初始化失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    /**
     * 手动滚动库存窗口
     */
    @PostMapping("/roll")
    public ResponseEntity<Map<String, Object>> rollInventoryWindow() {
        try {
            LocalDate newBaseDate = LocalDate.now();
            trainSeatService.rollInventoryWindow(newBaseDate);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "库存窗口滚动成功");
            response.put("newBaseDate", newBaseDate.toString());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "库存窗口滚动失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    /**
     * 清理过期库存记录
     */
    @DeleteMapping("/cleanup")
    public ResponseEntity<Map<String, Object>> cleanupExpiredInventory() {
        try {
            LocalDate expireDate = LocalDate.now().minusDays(7);
            trainSeatService.cleanupExpiredInventory(expireDate);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "过期库存清理成功");
            response.put("expireDate", expireDate.toString());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "过期库存清理失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    /**
     * 获取库存状态信息
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getInventoryStatus() {
        try {
            LocalDate today = LocalDate.now();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("today", today.toString());
            response.put("message", "库存状态查询成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "库存状态查询失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
}
